/**
 * 
 */
package profile.report;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author ductrung.tran
 *
 */
public class MethodResultReport {

	private String method;
	
	private Map<String, ParamResultReport> firstParamResultReportMap = new HashMap<String, ParamResultReport>();
	
	private Map<String, ParamResultReport> secondParamResultReportMap = new HashMap<String, ParamResultReport>();

	public void setMethod(String method) {
		this.method = method;
	}
	
	public String getMethod() {
		return method;
	}

	public void addFirstParamResultReport(ParamResultReport paramResultReport) {
		firstParamResultReportMap.put(paramResultReport.getParam(), paramResultReport);
	}

	public ParamResultReport getFirstParamResultReport(String param) {
		return firstParamResultReportMap.get(param);
	}

	public String toString(int level) {
		return shift(level) + method + "\n" + toStringParamResultReport(level);
	}

	private String shift(int level) {
		StringBuilder builder = new StringBuilder();
		for (int i = 1; i <= level; i++) {
			builder.append("  ");
		}
		return builder.toString();
	}

	private String toStringParamResultReport(int level) {
		StringBuilder builder = new StringBuilder();
		for (ParamResultReport paramResultReport : firstParamResultReportMap.values()) {
			builder.append(paramResultReport.toString(level+1)).append('\n');
		}
		
		for (ParamResultReport paramResultReport : secondParamResultReportMap.values()) {
			builder.append(paramResultReport.toString(level+1)).append('\n');
		}
		
		return builder.toString();
	}
	
	public Collection<ParamResultReport> getFirstParamResultReports() {
		return firstParamResultReportMap.values();
	}

	public void addSecondParamResultReport(
			ParamResultReport paramResultReport) {
		secondParamResultReportMap.put(paramResultReport.getParam(), paramResultReport);
	}
	
	public MethodCompareReport getCompareReport() {
		MethodCompareReport result = new MethodCompareReport();
		
		
		HashMap<String, ParamResultReport> clonedFirstParamResultReportMap = new HashMap<String, ParamResultReport>(firstParamResultReportMap);
		HashMap<String, ParamResultReport> clonedSecondParamResultReportMap = new HashMap<String, ParamResultReport>(secondParamResultReportMap);
		
		for (Iterator<Entry<String, ParamResultReport>> iter = clonedFirstParamResultReportMap.entrySet().iterator(); iter.hasNext();) {
			Entry<String, ParamResultReport> firstEntry = iter.next();
			ParamResultReport secondParamTestResultReport = clonedSecondParamResultReportMap.remove(firstEntry.getKey());
			if (secondParamTestResultReport != null) {
				result.addCommonParamCompareReport(firstEntry.getValue(), secondParamTestResultReport);
				iter.remove();
			}
		}
		
		result.addOtherParamReports(clonedFirstParamResultReportMap.values(), clonedSecondParamResultReportMap.values());
		
		return result;
	}
}
